<?php

// +----------------------------------------------------------------------

// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.

// +----------------------------------------------------------------------

// | Author: Dean <zxxjjforever@163.com>

// +----------------------------------------------------------------------

namespace plugins\module_config\model; //Demo插件英文名，改成你的插件英文就行了

use app\common\lib\file_operate\FileOperate;
use app\common\lib\module_config\TagRW;
use think\Config;
use think\Db;
use think\Model;

//Demo插件英文名，改成你的插件英文就行了,插件数据表最好加个plugin前缀再加表名,这个类就是对应“表前缀+plugin_demo”表

class PluginApiIndexModel extends Model
{

    protected $table = "cmf_module_config";

    /**

     * 获取模块配置

     * @param $name 模块名

     * @param $file_name 文件名 为空时只取出配置文件列表

     */

    public function getModuleTags($name, $file_name = null)
    {

        $TagRW = new TagRW();

        $name = cmf_parse_name($name);

        $result = $TagRW->getTag($name, $file_name);

        return $result;

    }

    /**

     * 保存模块配置

     * @param $module_name 模块名

     * @param $file_name 文件名

     * @param $data 数据

     */

    public function saveTags($module_name, $file_name, $data)
    {

        $TagRW = new TagRW();

        $module_name = cmf_parse_name($module_name);

        $result = $TagRW->setTag($module_name, $file_name, $data);

        return $result;

    }

    /**

     * 获取公共模块配置

     */

    public function getTagsConfig()
    {

        $file = new FileOperate();

        $result = $file->readConfigFile('config', 'tags.php');

        return $result;

    }

    /**

     * 获取主表字段信息

     */

    public function getMainTable($module_name = '')
    {

        $prefix = config('database.prefix');

        try {

            $table = Db::getTableFields($prefix . cmf_parse_name($module_name));

        } catch (\Exception $e) {

            return '';

        }

        return $table;

    }

    /**

     * 获取主表名

     */

    public function getTableName($module_name = '')
    {

        $prefix = config('database.prefix');

        $table_name = $prefix . cmf_parse_name($module_name);

        return $table_name;

    }

    /**

     * 获取模块的数据表

     */

    public function getTables($module_name = '')
    {

        $database = config('database.database');

        $prefix = config('database.prefix');

        $table = Db::query('select table_name from information_schema.tables where table_schema = "' . $database . '"');

        $table = array_column($table, 'table_name');

        if (!empty($module_name)) {

            $module_name = $prefix . cmf_parse_name($module_name);

            $table = array_filter($table, function ($key) use ($module_name) {

                return strpos($key, $module_name) === 0;

            }, ARRAY_FILTER_USE_BOTH);

        }

        return $table;

    }

}
